import http from '@ohos.net.http'

import Logger from '../model/Logger'

const TAG = '[aaaaa]'

export class HuaweiCloudApi {
    IAMEndpoint: string
    IOTDAEndpoint: string
    IAMUserName: string
    IAMPassword: string
    IAMDoaminId: string
    region: string
    X_Auth_Token: Object
    project_id: string
    product_id: string
    device_id: string

    requestData: http.HttpResponse = undefined

    constructor() {}

    async request(uri: string, op: Object) {
        let httpRequest = http.createHttp()
        Logger.info(TAG, `createHttp uri = ${uri}`)
        try {
            let result = await httpRequest.request(uri, op) //JS API 网络数据请求
            Logger.info(TAG, `HttpResponse's result is ${JSON.stringify(result.result)}`)
            Logger.info(TAG, `responseCode is ${result.responseCode} header is ${JSON.stringify(result.header)}
        cookies is ${JSON.stringify(result.cookies)}}`)
            return result
        } catch (err) {
            Logger.info(TAG, `This err is ${JSON.stringify(err)}`)
            httpRequest.destroy()
            return err
        }
    }

    async getIAMUserToken(){
        let PostHeader = {
            'Content-Type': 'application/json'
        }
        let PostBody = {
            "auth": {
                "identity": {
                    "methods": [
                        "password"
                    ],
                    "password": {
                        "user": {
                            "name": this.IAMUserName,
                            "password": this.IAMPassword,
                            "domain": {
                                "name": this.IAMDoaminId
                            }
                        }
                    }
                },
                "scope": {
                    "project": {
                        "name": this.region
                    }
                }
            }
        }
        /* 官方文档：https://support.huaweicloud.com/api-iam/iam_30_0001.html*/
        let requestData = await this.request('https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens', { //发起网络数据请求,url/请求头
            method: 'POST',
            extraData: PostBody,  // 请求体
            header: PostHeader,
            readTimeout: 5000,
            connectTimeout: 5000,
        })
        Logger.info(TAG, `getIAMUserToken header is ${JSON.stringify(requestData.header)}`)//响应头.Object类型
        Logger.info(TAG, `getIAMUserToken result is ${JSON.stringify(requestData.result)}`)//相应体.string类型
        return requestData.header['X-Subject-Token']
    }
    /* 官方文档：https://support.huaweicloud.com/api-iothub/iot_06_v5_0079.html*/
    async showDeviceShadow(){
        let PostHeader = {
            'Content-Type': 'application/json',
            'X-Auth-Token': this.X_Auth_Token
        }
        let PostBody = {}
        let requestData = await this.request('https://iotda.cn-north-4.myhuaweicloud.com/v5/iot/'+this.project_id+'/devices/'+this.device_id+'/shadow', { //发起网络数据请求,url/请求头
            method: 'GET',
            extraData: PostBody,  // 请求体
            header: PostHeader,
            readTimeout: 5000,
            connectTimeout: 5000,
        })
        Logger.info(TAG, `showDeviceShadow header is ${JSON.stringify(requestData.header)}`)//响应头.Object类型
        Logger.info(TAG, `showDeviceShadow result is ${JSON.stringify(requestData.result)}`)//相应体.string类型
//        return JSON.stringify(JSON.parse(requestData.result).shadow[0].reported.properties.InVoltage)
        return JSON.parse(requestData.result).shadow[0].reported.properties
    }

}

export default new HuaweiCloudApi()